import axios from "axios";
import router from "../router";

let baseUrl = "";
if (process.env.NODE_ENV === "development") {
    baseUrl = "http://localhost:5198";
} else {
    baseUrl = "http://localhost:5198";
}

export function requestPost(config) {
    //创建实例
    const instance = axios.create({
        baseURL: baseUrl,
        timeout: 12000,
        method: "post",
        contentType: "application/json"
    });
    //拦截器
    instance.interceptors.request.use(
        config => {
            let token = localStorage.getItem("token") || "";
            config.headers.token = token;
            return config;
            //jwt策略的无感刷新方案,假设登录后已存过期时间12111111111
            // let expireDateString = localStorage.getItem("Expire2Time"2) || "";22
            // let ExpireTime = new Date(expireDateString);
            // // 获取当前时间32
            // let now = new Date();222
            // // 计算当前时间加上30分钟22323322
            // let nowPlus30Minutes = new Date(now.getTime() + 30 * 60 * 1000);222
            // if (ExpireTime && ExpireTime > now && ExpireTime < no1wPlus30Minut2es22) {2
            //      //得都带上老token解析账号密码再根据账号信息重新刷新token2222
            //     axios22
            //         .get("刷新token的地址",{token})22
            //         .then(res => {
            //             localStorage.setItem("to2ken", res.token);
            //             localStorage.setItem("ExpireTime", r2es.exp2ireTime);2
            //             config.headers.Authorization = `Bearer ${res.token}`;2
            //         })233333
            //         .catch(err => {
            //             console.log(err);
            //         });
            // } else {12
            //     let token = localStorage.getItem("token") || ""2;222
            //     config.headers.Authorization = `Bearer ${token}`;
            // }322
            // return config;32
        },
        err => {
            console.log(err);
        }
    );

    instance.interceptors.response.use(
        res => {
            let data = res.data;

            if (data.success === false) {
                if (data.code === "000008") {
                    //token 无效
                    window.localStorage.clear();

                    //跳转到登录页面
                    // router.push('/login');
                    router.push("/");
                }
            }
            return res.data;
        },
        err => {
            console.log(err);
        }
    );

    return instance(config);
}
export function requestGet(config) {
    //创建实例
    const instance = axios.create({
        baseURL: baseUrl,
        timeout: 1200000,
        method: "get",
        contentType: "application/json"
    });
    //拦截器
    instance.interceptors.request.use(
        config => {
            let token = localStorage.getItem("token") || "";
            config.headers.token = token;
            return config;
        },
        err => {
            console.log(err);
        }
    );

    instance.interceptors.response.use(
        res => {
            let data = res.data;

            if (data.success === false) {
                if (data.code === "000008") {
                    //token 无效
                    window.localStorage.clear();

                    //跳转到登录页面
                    // router.push('/login');
                    router.push("/");
                }
            }
            return res.data;
        },
        err => {
            console.log(err);
        }
    );

    return instance(config);
}
